﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;

namespace Agresso.Interface.Layer.Domain
{
    public class SupplierCustomer
    {
        public long supp_cust_id;
        public string co_type = string.Empty;
        public string short_code = string.Empty;
        public string short_name = string.Empty;
        public string full_name = string.Empty;
        public string full_address = string.Empty;
        public string city = string.Empty;
        public string country = string.Empty;
        public string account_code = string.Empty;
        public string active = string.Empty;

        public string agr_supp_apar_id = string.Empty;
        public string agr_supp_apar_name = string.Empty;
        //public string agr_supp_profitcentre_dim_value = string.Empty;

        public string agr_cust_apar_id = string.Empty;
        public string agr_cust_apar_name = string.Empty;
        public string agr_cust_rel_value = string.Empty;

        public bool interco = false;
        public string sup_account = string.Empty;
        //public string legal_entity_name = string.Empty;
        public string interco_apar_id = string.Empty;
        public string cus_account = string.Empty;
        public string cost_centre_chartering = string.Empty;
        public string client = string.Empty;
        public string cust_client = string.Empty;
        public string apar_gr_id = string.Empty;
        public string cus_apar_gr_id = string.Empty;
        public ProfitCentre ProfitCentre = null;

        public DateTime created_date = new DateTime(1900, 1, 1);
        public DateTime modified_date = new DateTime(1900, 1, 1);

        public string country_code_sup = "SG";
        public string country_code_cus = "SG";


        public SupplierCustomer() { }
        public SupplierCustomer(bool isFromSoftmar, SqlDataReader rd)
        {
            if (!isFromSoftmar)
            {
                supp_cust_id = Convert.ToInt32(rd["supp_cust_id"]);
                agr_supp_apar_id = rd["agr_supp_apar_id"] is DBNull ? agr_supp_apar_id : rd["agr_supp_apar_id"].ToString();
                agr_supp_apar_name = rd["agr_supp_apar_name"] is DBNull ? agr_supp_apar_name : rd["agr_supp_apar_name"].ToString();
               // agr_supp_profitcentre_dim_value = rd["agr_supp_rel_value"] is DBNull ? agr_supp_profitcentre_dim_value : rd["agr_supp_rel_value"].ToString();
                
                agr_cust_apar_id = rd["agr_cust_apar_id"] is DBNull ? agr_cust_apar_id : rd["agr_cust_apar_id"].ToString();
                agr_cust_apar_name = rd["agr_cust_apar_name"] is DBNull ? agr_cust_apar_name : rd["agr_cust_apar_name"].ToString();
                agr_cust_rel_value = rd["agr_cust_rel_value"] is DBNull ? agr_cust_rel_value : rd["agr_cust_rel_value"].ToString();

                created_date = rd["created_date"] is DBNull ? created_date : Convert.ToDateTime(rd["created_date"]);
                modified_date = rd["modified_date"] is DBNull ? modified_date : Convert.ToDateTime(rd["modified_date"]);

                interco = rd["interco"] is DBNull ? interco : Convert.ToBoolean(rd["interco"]);
                sup_account = rd["sup_account"] is DBNull ? sup_account : rd["sup_account"].ToString();
                cus_account = rd["cus_account"] is DBNull ? cus_account : rd["cus_account"].ToString();
                cost_centre_chartering = rd["cost_centre_chartering"] is DBNull ? cost_centre_chartering : rd["cost_centre_chartering"].ToString();
                client = rd["client"] is DBNull ? client : rd["client"].ToString();
                cust_client = rd["cust_client"] is DBNull ? cust_client : rd["cust_client"].ToString();

               // legal_entity_name = rd["legal_entity_name"] is DBNull ? legal_entity_name : rd["legal_entity_name"].ToString();
                interco_apar_id = rd["interco_apar_id"] is DBNull ? interco_apar_id : rd["interco_apar_id"].ToString();
                ProfitCentre = new ProfitCentre(rd["legal_entity_id"] is DBNull ? 0 : Convert.ToInt32(rd["legal_entity_id"]));

                apar_gr_id = rd["apar_gr_id"] is DBNull ? apar_gr_id : rd["apar_gr_id"].ToString();
                cus_apar_gr_id = rd["cus_apar_gr_id"] is DBNull ? cus_apar_gr_id : rd["cus_apar_gr_id"].ToString();
            }
            co_type = rd["co_type"] is DBNull ? co_type : rd["co_type"].ToString();
            short_code = rd["short_code"] is DBNull ? short_code : rd["short_code"].ToString();
            short_name = rd["short_name"] is DBNull ? short_name : rd["short_name"].ToString();
            full_name = rd["full_name"] is DBNull ? full_name : rd["full_name"].ToString();
            full_address = rd["full_address"] is DBNull ? full_address : rd["full_address"].ToString();
            city = rd["city"] is DBNull ? city : rd["city"].ToString();
            country = rd["country"] is DBNull ? country : rd["country"].ToString();
            account_code = rd["account_code"] is DBNull ? account_code : rd["account_code"].ToString();
            active = rd["active"] is DBNull ? active : rd["active"].ToString();
        }

        public bool IsValidSupplier()
        {
            if (interco)
            {
                return
                    ProfitCentre.profit_centre_id > 0 &&
                    ProfitCentre.client != string.Empty &&
                    ProfitCentre.agr_dim_value != string.Empty &&
                    interco_apar_id != string.Empty &&
                    sup_account != string.Empty;
            }
            return agr_supp_apar_id != string.Empty;
        }

        public bool IsValidCustomer()
        {
            return agr_cust_apar_id != string.Empty && agr_cust_apar_name != string.Empty && agr_cust_rel_value != string.Empty && cus_account != string.Empty && client != string.Empty;
        }
    }

}
